<?php

namespace app\api\controller;

use app\api\model\Article as ArticleModel;
use app\api\model\article\Category as CategoryModel;
use think\Db;

/**
 * 文章控制器
 * Class Article
 * @package app\api\controller
 */
class Article extends Controller
{
    /**
     * 文章首页
     * @return array
     */
    public function index()
    {
        // 文章分类列表
        $categoryList = CategoryModel::getAll();
        return $this->renderSuccess(compact('categoryList'));
    }

    /**
     * 文章列表
     * @param int $category_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function lists($category_id = 0)
    {
        $model = new ArticleModel;
        $list = $model->getList($category_id, 100);
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 文章详情
     * @param $article_id
     * @return array
     * @throws \app\common\exception\BaseException
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function detail($article_id)
    {
        $detail = ArticleModel::detail($article_id);
        return $this->renderSuccess(compact('detail'));
    }

    /**获取分类信息详情，里面包含投票信息和说明文字 */
    public function getTypeObj($category_id){
        $domain = $this->request->domain();
        $detail = Db::name('article_category')->where(['category_id'=>$category_id])->find();

        $imageObj = Db::name('upload_file')->where(['file_id'=>isset($detail['image_id']) ? $detail['image_id'] : 0])->find();
        $imagePath = $imageObj['file_img_allpath']!="" ? $imageObj['file_img_allpath'] : $domain.'/uploads/'.$imageObj['file_name'];
        $detail['image'] = ['file_path'=>$imagePath];

        return $this->renderSuccess(compact('detail'));
    }

    /**增加文章的投票量*/
    public function addtoupiao(){
        $paramArr = $this->request->param();
        $tpartid = isset($paramArr['tpartid']) ? $paramArr['tpartid'] : 0;
        $userid = isset($paramArr['userid']) ? $paramArr['userid'] : 0;
        if($tpartid>0 && $userid>0){
            $dayStr = date('Y-m-d',time());
            // 设置投票活动的结束时间
            $overTimeInt = strtotime('2024-12-10 23:59:59');
            if(time() > $overTimeInt){
                $back['isok'] = 'no';
                $back['msg'] = '该投票活动已结束';
                return $this->renderSuccess(compact('back'));
            }
            // 查找今天是否已经投过票了
            $toupiaoObj = Db::name('article_toupiaolog')->where(['atp_artid'=>$tpartid, 'atp_userid'=>$userid, 'atp_day'=>$dayStr])->find();
            if($toupiaoObj){
                $back['isok'] = 'no';
                $back['msg'] = '您今天已给Ta投过了，明天再来哦';  //'您已给Ta投过了，不能重复投票'
                return $this->renderSuccess(compact('back'));
            }
            else{
                $inArr['atp_artid'] = $tpartid;
                $inArr['atp_userid'] = $userid;
                $inArr['wxapp_id'] = $this->wxapp_id;
                $inArr['atp_day'] = date('Y-m-d',time());
                $inArr['atp_addtime'] = date('Y-m-d H:i:s',time());
                $res = Db::name('article_toupiaolog')->insert($inArr);

                //修改票数
                $sql = "update ".getTable('article')." set get_piaonum=get_piaonum+1 where article_id=".$tpartid;
                $res2 = Db::query($sql);

                $wzObj = Db::name('article')->where(['article_id'=>$tpartid])->field('get_piaonum')->find();
                $back['isok'] = 'yes';
                $back['piaoNum'] = $wzObj['get_piaonum'];
                return $this->renderSuccess(compact('back'));
            }
        }
        else{
            return $this->renderError('缺少投票参数');
        }
    }

}
